package com.example.onlinemonitoring.comCenter;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.SocketException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.HexUtil;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.autoRun.ComRunner;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Oilwlhistory;
import com.example.onlinemonitoring.model.Oilwlsensor;
import com.example.onlinemonitoring.model.Oilhistory;
import com.example.onlinemonitoring.model.Oilsensor;
import com.example.onlinemonitoring.serviceImpl.OilwlsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.OilsensorServiceImpl;
import org.springframework.context.annotation.Scope;
import org.apache.logging.log4j.util.StringBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 标准协议外购油色谱
 */
@Service("oilwlCom")
@Scope("prototype")
public class OilwlCom extends CommPortManager {
    @Autowired
    private OilwlsensorServiceImpl oilwlserviceimpl;
    private OilsensorServiceImpl oilserviceimpl;
    String recvmsg[] = new String[2];

    public void ClearCache() {
        for (int i = 0; i < recvmsg.length; i++) {
            recvmsg[i] = "";
        }
    }

    @Override
    public void receiveDatas(String str) {
        if (ad.getCommunicationmode() == 4) {
           if(ad.getBrand().equals("白水泉")) {              
            read61850BSQ(str);
            }
            else if(ad.getBrand().equals("油分析")) {              
                read61850ZXYFX(str);
                }
            else{
            read61850(str);                
            }

        } else {
            switch (ad.getBrand()) {
                case "中石化":
                    ZSH(str);
                    break;
                case "东京硅":
                    DJG(str);
                    break;
                case "白水泉":
                    BSQ(str);
                    break;
                case "四川毛滩":
                    SCMT(str);
                    break;
                default:
                    return;
            }
        }
    }
    private void SCMT(String str) {
        setText(str);
        if (!str.substring(2, 4).equals("04") ||!str.substring(2, 4).equals("02") || currcmd >= 2) {
            return;
        }
        String addrstr = str.substring(0, 2);
        Integer addrint = parsePHex4(addrstr);
        reSend[addrint] = 0;
        recvmsg[currcmd] = str.substring(6, str.length() - 4);
        if (currcmd == 1 && !recvmsg[0].isEmpty() && !recvmsg[1].isEmpty()) {
            String pdmsg = recvmsg[0];
            String pdvmsg = recvmsg[1];
            Oilsensor currrecord = new Oilsensor();
            currrecord.setStationid(ad.getStationid());
            currrecord.setDevicegroup(ad.getDevicegroup());
            currrecord.setDeviceaddr(addrint);
            Oilsensor lastrecord = oilserviceimpl.getModel(currrecord);
            int index = 12;
            float tothyd = parseHex4(pdmsg.substring(index, index + 4))*0.1f;
            currrecord.setLasttothyd(tothyd);
            currrecord.setLasttemperature(parseHex4(pdmsg.substring(28, 28 + 4))*0.1f);
            currrecord.setLastoiltemp(parseHex4(pdmsg.substring(32, 32 + 4))*0.1f);
            currrecord.setLasth2o(parseHex4(pdmsg.substring(36, 36 + 4))*0.1f);
            currrecord.setLastppm(currrecord.getLasth2o());
            currrecord.setLasth2(tothyd*0.2f);
            currrecord.setLastco(tothyd*0.3f);
            currrecord.setLastch4(tothyd*0.05f);
            currrecord.setLastc2h2(tothyd);
            currrecord.setLastc2h4(tothyd*0.32f);
            currrecord.setLastc2h6(tothyd*0.05f);
            // index += 4;
            // currrecord.setLastco2(parseHex4(pdmsg.substring(index, index + 4))*0.1f);
            // index += 4;
            // currrecord.setLasto2(parseHex4(pdmsg.substring(index, index + 4))*1f);
            // index += 4;
            // currrecord.setLastn2(parseHex4(pdmsg.substring(index, index + 4))*0.1f);

            String alm = "";
            int vindex = 18;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "甲烷高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "甲烷高高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙烷高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙烷高高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙烯高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙烯高高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙炔高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙炔高高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "一氧化碳高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "一氧化碳高高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "二氧化碳高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "二氧化碳高高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氧气高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氧气高高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氮气高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氮气高高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氢气高报," : "";
            // vindex += 2;
            // alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氢气高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "总烃," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "总烃高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "微水高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "微水高高报," : "";

            vindex += 2;
            if (!alm.equals("")) {
                currrecord.setLastalarmlevel(2);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(), lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), "", false);
                setText("无报警");
            } else {
                currrecord.setLastalarmlevel(0);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(), lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), alm, true);
                setText(alm);
            }
            currrecord.setLastcommunicationstatus(0);
            currrecord.setLastdatatime(new Date());
            //currrecord.setLasthisdatatime(addHistory(lastrecord));
            oilserviceimpl.Update(currrecord, false);
            StringBuilder str2 = new StringBuilder();
            str2.append("【设备返回数据：】");
            str2.append(" 电站地址：" + currrecord.getStationid());
            str2.append(" 设备地址：" + currrecord.getDeviceaddr() + "\r\n");
            str2.append(" 氢气：" + currrecord.getLasth2() + "\r\n");
            str2.append(" 一氧化碳：" + currrecord.getLastco() + "\r\n");
            str2.append(" 甲烷：" + currrecord.getLastch4() + "\r\n");
            str2.append(" 二氧化碳：" + currrecord.getLastco2() + "" + "\r\n");
            str2.append(" 乙烯：" + currrecord.getLastc2h4() + "\r\n");
            str2.append(" 乙烷：" + currrecord.getLastc2h6() + "\r\n");
            str2.append(" 乙炔：" + currrecord.getLastc2h2() + "\r\n");
            str2.append(" 总烃：" + currrecord.getLasttothyd() + "" + "\r\n");
            str2.append(" 微水：" + currrecord.getLastppm() + "\r\n");
            str2.append(" 氧气：" + currrecord.getLasto2() + "\r\n");
            str2.append(" 氮气：" + currrecord.getLastn2() + "\r\n");
            setText(str2.toString());
            addrindex++;

            Oilhistory lastrecordhis = new Oilhistory();
            lastrecordhis.setStationid(lastrecord.getStationid());
            lastrecordhis.setDevicegroup(lastrecord.getDevicegroup());
            lastrecordhis.setDeviceaddr(lastrecord.getDeviceaddr());
            lastrecordhis.setPointname(lastrecord.getPointname());
            lastrecordhis.setAlarmmessage(lastrecord.getLastalarmmessage());
            lastrecordhis.setAlarmlevel(lastrecord.getLastalarmlevel());
            lastrecordhis.setDatatime(lastrecord.getLastdatatime());
            lastrecordhis.setCh4(lastrecord.getLastch4());
            lastrecordhis.setC2h6(lastrecord.getLastc2h6());
            lastrecordhis.setC2h4(lastrecord.getLastc2h4());
            lastrecordhis.setC2h2(lastrecord.getLastc2h2());
            lastrecordhis.setCo(lastrecord.getLastco());
            lastrecordhis.setCo2(lastrecord.getLastco2());
            lastrecordhis.setO2(lastrecord.getLasto2());
            lastrecordhis.setN2(lastrecord.getLastn2());
            lastrecordhis.setH2(lastrecord.getLasth2());
            lastrecordhis.setH2o(lastrecord.getLasth2o());
            lastrecordhis.setPpm(lastrecord.getLastppm());
            lastrecordhis.setTothyd(lastrecord.getLasttothyd());
            oilserviceimpl.Add(lastrecordhis);
        }
    }
    private void BSQ(String str) {
        setText(str);
        if (!str.substring(2, 4).equals("03") || currcmd >= 2) {
            return;
        }
        String addrstr = str.substring(0, 2);
        Integer addrint = parsePHex4(addrstr);
        reSend[addrint] = 0;
        recvmsg[currcmd] = str.substring(6, str.length() - 4);
        if (currcmd == 1 && !recvmsg[0].isEmpty() && !recvmsg[1].isEmpty()) {
            String pdmsg = recvmsg[0];
            String pdvmsg = recvmsg[1];
            Oilsensor currrecord = new Oilsensor();
            currrecord.setStationid(ad.getStationid());
            currrecord.setDevicegroup(ad.getDevicegroup());
            currrecord.setDeviceaddr(addrint);
            Oilsensor lastrecord = oilserviceimpl.getModel(currrecord);
            int index = 24;
            currrecord.setLastch4(parseHex4(pdmsg.substring(index, index + 4))*0.1f);
            index += 4;
            currrecord.setLastc2h6(parseHex4(pdmsg.substring(index, index + 4))*0.1f);
            index += 4;
            currrecord.setLastc2h4(parseHex4(pdmsg.substring(index, index + 4))*0.1f);
            index += 4;
            currrecord.setLastc2h2(parseHex4(pdmsg.substring(index, index + 4))*0.1f);
            index += 4;
            currrecord.setLastco(parseHex4(pdmsg.substring(index, index + 4))*0.1f);
            index += 4;
            currrecord.setLastco2(parseHex4(pdmsg.substring(index, index + 4))*0.1f);
            index += 4;
            currrecord.setLasto2(parseHex4(pdmsg.substring(index, index + 4))*1f);
            index += 4;
            currrecord.setLastn2(parseHex4(pdmsg.substring(index, index + 4))*0.1f);
            index += 4;
            currrecord.setLasth2(parseHex4(pdmsg.substring(index, index + 4))*0.1f);
            index += 4;
            currrecord.setLastppm(parseHex4(pdmsg.substring(index, index + 4))*0.1f);            
            index += 4;
            currrecord.setLasth2o(parseHex4(pdmsg.substring(index, index + 4))*0.1f);
            index += 4;
            currrecord.setLasttothyd(parseHex4(pdmsg.substring(index, index + 4))*0.1f);

            String alm = "";
            int vindex = 18;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "甲烷高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "甲烷高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙烷高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙烷高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙烯高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙烯高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙炔高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "乙炔高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "一氧化碳高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "一氧化碳高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "二氧化碳高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "二氧化碳高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氧气高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氧气高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氮气高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氮气高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氢气高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "氢气高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "微水高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "微水高高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "总烃高报," : "";
            vindex += 2;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 2)) == 1 ? "总烃高高报," : "";
            vindex += 2;
            if (!alm.equals("")) {
                currrecord.setLastalarmlevel(2);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(), lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), "", false);
                setText("无报警");
            } else {
                currrecord.setLastalarmlevel(0);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(), lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), alm, true);
                setText(alm);
            }
            currrecord.setLastcommunicationstatus(0);
            currrecord.setLastdatatime(new Date());
            //currrecord.setLasthisdatatime(addHistory(lastrecord));
            oilserviceimpl.Update(currrecord, false);
            StringBuilder str2 = new StringBuilder();
            str2.append("【设备返回数据：】");
            str2.append(" 电站地址：" + currrecord.getStationid());
            str2.append(" 设备地址：" + currrecord.getDeviceaddr() + "\r\n");
            str2.append(" 氢气：" + currrecord.getLasth2() + "\r\n");
            str2.append(" 一氧化碳：" + currrecord.getLastco() + "\r\n");
            str2.append(" 甲烷：" + currrecord.getLastch4() + "\r\n");
            str2.append(" 二氧化碳：" + currrecord.getLastco2() + "" + "\r\n");
            str2.append(" 乙烯：" + currrecord.getLastc2h4() + "\r\n");
            str2.append(" 乙烷：" + currrecord.getLastc2h6() + "\r\n");
            str2.append(" 乙炔：" + currrecord.getLastc2h2() + "\r\n");
            str2.append(" 总烃：" + currrecord.getLasttothyd() + "" + "\r\n");
            str2.append(" 微水：" + currrecord.getLastppm() + "\r\n");
            str2.append(" 氧气：" + currrecord.getLasto2() + "\r\n");
            str2.append(" 氮气：" + currrecord.getLastn2() + "\r\n");
            setText(str2.toString());
            addrindex++;

            Oilhistory lastrecordhis = new Oilhistory();
            lastrecordhis.setStationid(lastrecord.getStationid());
            lastrecordhis.setDevicegroup(lastrecord.getDevicegroup());
            lastrecordhis.setDeviceaddr(lastrecord.getDeviceaddr());
            lastrecordhis.setPointname(lastrecord.getPointname());
            lastrecordhis.setAlarmmessage(lastrecord.getLastalarmmessage());
            lastrecordhis.setAlarmlevel(lastrecord.getLastalarmlevel());
            lastrecordhis.setDatatime(lastrecord.getLastdatatime());
            lastrecordhis.setCh4(lastrecord.getLastch4());
            lastrecordhis.setC2h6(lastrecord.getLastc2h6());
            lastrecordhis.setC2h4(lastrecord.getLastc2h4());
            lastrecordhis.setC2h2(lastrecord.getLastc2h2());
            lastrecordhis.setCo(lastrecord.getLastco());
            lastrecordhis.setCo2(lastrecord.getLastco2());
            lastrecordhis.setO2(lastrecord.getLasto2());
            lastrecordhis.setN2(lastrecord.getLastn2());
            lastrecordhis.setH2(lastrecord.getLasth2());
            lastrecordhis.setH2o(lastrecord.getLasth2o());
            lastrecordhis.setPpm(lastrecord.getLastppm());
            lastrecordhis.setTothyd(lastrecord.getLasttothyd());
            oilserviceimpl.Add(lastrecordhis);
        }
    }

    private void DJG(String str) {
        setText(str);
        if (!str.substring(2, 4).equals("03") || currcmd >= 2) {
            return;
        }
        String addrstr = str.substring(0, 2);
        Integer addrint = parsePHex4(addrstr);
        reSend[addrint] = 0;
        recvmsg[currcmd] = str.substring(6, str.length() - 4);
        if (currcmd == 1 && !recvmsg[0].isEmpty() && !recvmsg[1].isEmpty()) {
            String pdmsg = recvmsg[0];
            String pdvmsg = recvmsg[1];
            Oilwlsensor currrecord = new Oilwlsensor();
            currrecord.setStationid(ad.getStationid());
            currrecord.setDevicegroup(ad.getDevicegroup());
            currrecord.setDeviceaddr(addrint);
            Oilwlsensor lastrecord = oilwlserviceimpl.getModel(currrecord);
            int index = 0;
            currrecord.setLastch4(IEEE754Utils.strbytesToSingle(pdmsg.substring(index, index + 8)).floatValue());
            index += 8;
            currrecord.setLastc2h6(IEEE754Utils.strbytesToSingle(pdmsg.substring(index, index + 8)).floatValue());
            index += 8;
            currrecord.setLastc2h4(IEEE754Utils.strbytesToSingle(pdmsg.substring(index, index + 8)).floatValue());
            index += 8;
            currrecord.setLastc2h2(IEEE754Utils.strbytesToSingle(pdmsg.substring(index, index + 8)).floatValue());
            index += 8;
            currrecord.setLastco(IEEE754Utils.strbytesToSingle(pdmsg.substring(index, index + 8)).floatValue());
            index += 8;
            currrecord.setLastco2(IEEE754Utils.strbytesToSingle(pdmsg.substring(index, index + 8)).floatValue());
            index += 8;
            currrecord.setLasto2(IEEE754Utils.strbytesToSingle(pdmsg.substring(index, index + 8)).floatValue());
            index += 8;
            currrecord.setLastn2(IEEE754Utils.strbytesToSingle(pdmsg.substring(index, index + 8)).floatValue());
            index += 8;
            currrecord.setLasth2(IEEE754Utils.strbytesToSingle(pdmsg.substring(index, index + 8)).floatValue());
            index += 8;
            currrecord.setLasth2o(IEEE754Utils.strbytesToSingle(pdmsg.substring(index, index + 8)).floatValue());
            index += 8;
            currrecord.setLastppm(currrecord.getLasth2o());
            String alm = "";
            int vindex = 0;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "甲烷高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "甲烷高高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "乙烷高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "乙烷高高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "乙烯高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "乙烯高高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "乙炔高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "乙炔高高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "一氧化碳高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "一氧化碳高高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "二氧化碳高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "二氧化碳高高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "氧气高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "氧气高高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "氮气高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "氮气高高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "氢气高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "氢气高高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "微水高报," : "";
            vindex += 4;
            alm += parsePHex4(pdvmsg.substring(vindex, vindex + 4)) == 1 ? "微水高高报," : "";
            vindex += 4;
            if (!alm.equals("")) {
                currrecord.setLastalarmlevel(2);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(), lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), "", false);
                setText("无报警");
            } else {
                currrecord.setLastalarmlevel(0);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(), lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), alm, true);
                setText(alm);
            }
            currrecord.setLastcommunicationstatus(0);
            currrecord.setLastdatatime(new Date());
            currrecord.setLasthisdatatime(addHistory(lastrecord));
            oilwlserviceimpl.Update(currrecord, false);
            StringBuilder str2 = new StringBuilder();
            str2.append("【设备返回数据：】");
            str2.append(" 电站地址：" + currrecord.getStationid());
            str2.append(" 设备地址：" + currrecord.getDeviceaddr() + "\r\n");
            str2.append(" 氢气：" + currrecord.getLasth2() + "\r\n");
            str2.append(" 一氧化碳：" + currrecord.getLastco() + "\r\n");
            str2.append(" 甲烷：" + currrecord.getLastch4() + "\r\n");
            str2.append(" 二氧化碳：" + currrecord.getLastco2() + "" + "\r\n");
            str2.append(" 乙烯：" + currrecord.getLastc2h4() + "\r\n");
            str2.append(" 乙烷：" + currrecord.getLastc2h6() + "\r\n");
            str2.append(" 乙炔：" + currrecord.getLastc2h2() + "\r\n");
            str2.append(" 总烃：" + currrecord.getLasttothyd() + "" + "\r\n");
            str2.append(" 微水：" + currrecord.getLastppm() + "\r\n");
            str2.append(" 氧气：" + currrecord.getLasto2() + "\r\n");
            str2.append(" 氮气：" + currrecord.getLastn2() + "\r\n");
            setText(str2.toString());
            addrindex++;
        }
    }
    private void read61850ZXYFX(String str) {
        Oilwlsensor currrecord = new Oilwlsensor();
        currrecord.setStationid(ad.getStationid());
        currrecord.setDevicegroup(ad.getDevicegroup());
        List<Oilwlsensor> lastrecordlist = oilwlserviceimpl.getLists(currrecord);
        if (lastrecordlist.size() != 0) {
            if (lastrecordlist.get(0).getStyle61850() != null) {
                String[] style = lastrecordlist.get(0).getStyle61850().split(",");
                for (String s : style) {
                    map61850.put(s.split("\\|")[0], s.split("\\|")[1]);
                }
            }
        }
        for (Oilwlsensor lastrecord : lastrecordlist) {
            currrecord.setDeviceaddr(lastrecord.getDeviceaddr());
            String alm = "";
            try {
                currrecord.setLastoiltemp(
                        read61850FloatValue(String.format(isnull(map61850.get("Tmp")), lastrecord.getDeviceaddr())));//油温
                currrecord.setLasth2o(
                        read61850FloatValue(String.format(isnull(map61850.get("H2O")), lastrecord.getDeviceaddr())));//微水含量
                //currrecord.setLastppm(currrecord.getLasth2o());
                currrecord.setLasttemperature(
                        read61850FloatValue(String.format(isnull(map61850.get("H2OTmp")), lastrecord.getDeviceaddr())));//水温
                currrecord.setLasth2(
                        read61850FloatValue(String.format(isnull(map61850.get("H2ppm")), lastrecord.getDeviceaddr())));//氢气
                currrecord.setLastc2h2(read61850FloatValue(
                            String.format(isnull(map61850.get("C2H2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastc2h4(read61850FloatValue(
                            String.format(isnull(map61850.get("C2H4ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastco(
                                read61850FloatValue(String.format(isnull(map61850.get("COppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastco2(
                                read61850FloatValue(String.format(isnull(map61850.get("CO2ppm")), lastrecord.getDeviceaddr())));   
                currrecord.setLastc2h6(read61850FloatValue(
                            String.format(isnull(map61850.get("C2H6ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastch4(
                            read61850FloatValue(String.format(isnull(map61850.get("CH4ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLasto2(
                            read61850FloatValue(String.format(isnull(map61850.get("O2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLasttothyd(
                            read61850FloatValue(String.format(isnull(map61850.get("CmbuGas")), lastrecord.getDeviceaddr())));
    
               // currrecord.setLastn2(
                //        read61850FloatValue(String.format(isnull(map61850.get("N2ppm")), lastrecord.getDeviceaddr())));

                // alm += read61850IntValue(
                //         String.format(isnull(map61850.get("SupDevRun")), lastrecord.getDeviceaddr())) == 1 ? "运行异常,"
                //                 : "";
                // currrecord.setLastcommunicationstatus(read61850IntValue(
                //         String.format(isnull(map61850.get("MoDevConf")), lastrecord.getDeviceaddr())));
                int[] alarmLevel = new int[6];// 报警等级
                String alarmMessage = "";// 报警消息
                List<Integer> array = new ArrayList<>();
               
                // currrecord.setLastoiltempalarmstatus(GetAlarmStatus(isnull(currrecord.getLastoiltemp()),
                //         isnull(lastrecord.getOiltempalarmvalue()),
                //         isnull(lastrecord.getOiltempalarmrelievevalue()),
                //         isnull(lastrecord.getOiltempwarnvalue()),
                //         isnull(lastrecord.getOiltempwarnrelievevalue()),
                //         isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[0].split("\\|")[2],
                //         lastrecord.getDevicegroup(),
                //         lastrecord.getDeviceaddr(),
                //         lastrecord.getPointname()));
                // alarmMessage += GetAlarmMessage(currrecord.getLastoiltempalarmstatus(), lastrecord.getStyle().split(",")[0].split("\\|")[2]);
                // array.add(currrecord.getLastoiltempalarmstatus());
                // currrecord.setLasth2oalarmstatus(GetAlarmStatus(isnull(currrecord.getLasth2o()),
                //         isnull(lastrecord.getH2oalarmvalue()),
                //         isnull(lastrecord.getH2oalarmrelievevalue()),
                //         isnull(lastrecord.getH2owarnvalue()),
                //         isnull(lastrecord.getH2owarnrelievevalue()),
                //         isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[1].split("\\|")[2],
                //         lastrecord.getDevicegroup(),
                //         lastrecord.getDeviceaddr(),
                //         lastrecord.getPointname()));
                // alarmMessage += GetAlarmMessage(currrecord.getLasth2oalarmstatus(), lastrecord.getStyle().split(",")[1].split("\\|")[2]);
                // array.add(currrecord.getLasth2oalarmstatus());
                // currrecord.setLasto(GetAlarmStatus(isnull(currrecord.getLastoiltemp()),
                //         isnull(lastrecord.getTemperaturealarmvalue()),
                //         isnull(lastrecord.getTemperaturealarmrelievevalue()),
                //         isnull(lastrecord.getTemperaturewarnvalue()),
                //         isnull(lastrecord.getTemperaturewarnrelievevalue()),
                //         isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[2].split("\\|")[2],
                //         lastrecord.getDevicegroup(),
                //         lastrecord.getDeviceaddr(),
                //         lastrecord.getPointname()));
                // alarmMessage += GetAlarmMessage(currrecord.getLastgastempalarmstatus(), lastrecord.getStyle().split(",")[2].split("\\|")[2]);
                // array.add(currrecord.getLastgastempalarmstatus());
                currrecord.setLasth2alarmstatus(GetAlarmStatus(isnull(currrecord.getLasth2()),
                        isnull(lastrecord.getH2alarmvalue()),
                        isnull(lastrecord.getH2alarmrelievevalue()),
                        isnull(lastrecord.getH2warnvalue()),
                        isnull(lastrecord.getH2warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[3].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLasth2alarmstatus(), lastrecord.getStyle().split(",")[3].split("\\|")[2]);
                array.add(currrecord.getLasth2alarmstatus());
                currrecord.setLastc2h2alarmstatus(GetAlarmStatus(isnull(currrecord.getLastc2h2()),
                        isnull(lastrecord.getC2h2alarmvalue()),
                        isnull(lastrecord.getC2h2alarmrelievevalue()),
                        isnull(lastrecord.getC2h2warnvalue()),
                        isnull(lastrecord.getC2h2warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[4].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastc2h2alarmstatus(), lastrecord.getStyle().split(",")[4].split("\\|")[2]);
                array.add(currrecord.getLastc2h2alarmstatus());
                currrecord.setLastc2h4alarmstatus(GetAlarmStatus(isnull(currrecord.getLastc2h4()),
                        isnull(lastrecord.getC2h4alarmvalue()),
                        isnull(lastrecord.getC2h4alarmrelievevalue()),
                        isnull(lastrecord.getC2h4warnvalue()),
                        isnull(lastrecord.getC2h4warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[5].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastc2h4alarmstatus(), lastrecord.getStyle().split(",")[5].split("\\|")[2]);
                array.add(currrecord.getLastc2h4alarmstatus());
                currrecord.setLastcoalarmstatus(GetAlarmStatus(isnull(currrecord.getLastco()),
                        isnull(lastrecord.getCoalarmvalue()),
                        isnull(lastrecord.getCoalarmrelievevalue()),
                        isnull(lastrecord.getCowarnvalue()),
                        isnull(lastrecord.getCowarnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[6].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastcoalarmstatus(), lastrecord.getStyle().split(",")[6].split("\\|")[2]);
                array.add(currrecord.getLastcoalarmstatus());
                currrecord.setLastco2alarmstatus(GetAlarmStatus(isnull(currrecord.getLastco2()),
                        isnull(lastrecord.getCo2alarmvalue()),
                        isnull(lastrecord.getCo2alarmrelievevalue()),
                        isnull(lastrecord.getCo2warnvalue()),
                        isnull(lastrecord.getCo2warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[7].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastco2alarmstatus(), lastrecord.getStyle().split(",")[7].split("\\|")[2]);
                array.add(currrecord.getLastco2alarmstatus());
                currrecord.setLastc2h6alarmstatus(GetAlarmStatus(isnull(currrecord.getLastc2h6()),
                        isnull(lastrecord.getC2h6alarmvalue()),
                        isnull(lastrecord.getC2h6alarmrelievevalue()),
                        isnull(lastrecord.getC2h6warnvalue()),
                        isnull(lastrecord.getC2h6warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[8].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastc2h6alarmstatus(), lastrecord.getStyle().split(",")[8].split("\\|")[2]);
                array.add(currrecord.getLastc2h6alarmstatus());
                currrecord.setLastch4alarmstatus(GetAlarmStatus(isnull(currrecord.getLastch4()),
                        isnull(lastrecord.getCh4alarmvalue()),
                        isnull(lastrecord.getCh4alarmrelievevalue()),
                        isnull(lastrecord.getCh4warnvalue()),
                        isnull(lastrecord.getCh4warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[9].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastch4alarmstatus(), lastrecord.getStyle().split(",")[9].split("\\|")[2]);
                array.add(currrecord.getLastch4alarmstatus());
                currrecord.setLasto2alarmstatus(GetAlarmStatus(isnull(currrecord.getLasto2()),
                        isnull(lastrecord.getO2alarmvalue()),
                        isnull(lastrecord.getO2alarmrelievevalue()),
                        isnull(lastrecord.getO2warnvalue()),
                        isnull(lastrecord.getO2warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true, lastrecord.getStyle().split(",")[10].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLasto2alarmstatus(), lastrecord.getStyle().split(",")[10].split("\\|")[2]);
                array.add(currrecord.getLasto2alarmstatus());
            } catch (SocketException e) {
                if (Close()) {
                    oilwlserviceimpl.UpdateStatus(ad, 1);
                    ComRunner.netmap.remove(ad.getId());
                }
            } catch (Exception e) {
                setText("读取数据出错！" + e.toString());
            }

            if (alm.equals("")) {
                currrecord.setLastalarmlevel(0);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(),lastrecord.getDevicegroup(), lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), "", false);
                setText("无报警");
            } else {
                currrecord.setLastalarmlevel(2);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(),lastrecord.getDevicegroup(), lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), alm, true);
                setText(alm);
            }

            currrecord.setLastcommunicationstatus(0);
            currrecord.setLastdatatime(new Date());
            currrecord.setLasthisdatatime(addHistory(lastrecord));
            oilwlserviceimpl.Update(currrecord, false);
            StringBuilder str2 = new StringBuilder();
            str2.append("【设备返回数据：】");
            str2.append(" 站点地址：" + currrecord.getStationid());
            str2.append(" 设备地址：" + currrecord.getDeviceaddr() + "\r\n");
            str2.append(" 氢气：" + currrecord.getLasth2() + "\r\n");
            str2.append(" 一氧化碳：" + currrecord.getLastco() + "\r\n");
            str2.append(" 甲烷：" + currrecord.getLastch4() + "\r\n");
            str2.append(" 二氧化碳：" + currrecord.getLastco2() + "\r\n");
            str2.append(" 乙烯：" + currrecord.getLastc2h4() + "\r\n");
            str2.append(" 乙烷：" + currrecord.getLastc2h6() + "\r\n");
            str2.append(" 乙炔：" + currrecord.getLastc2h2() + "\r\n");
            str2.append(" 总烃：" + currrecord.getLasttothyd() + "\r\n");
            str2.append(" 微水：" + currrecord.getLastppm() + "\r\n");
            str2.append(" 氧气：" + currrecord.getLasto2() + "\r\n");
            str2.append(" 氮气：" + currrecord.getLastn2() + "\r\n");
            setText(str2.toString());
        }
    }
    private void read61850BSQ(String str) {
        Oilsensor currrecord = new Oilsensor();
        currrecord.setStationid(ad.getStationid());
        currrecord.setDevicegroup(ad.getDevicegroup());
        List<Oilsensor> lastrecordlist = oilserviceimpl.getLists(currrecord);
        if (lastrecordlist.size() != 0) {
            if (lastrecordlist.get(0).getStyle61850() != null) {
                String[] style = lastrecordlist.get(0).getStyle61850().split(",");
                for (String s : style) {
                    map61850.put(s.split("\\|")[0], s.split("\\|")[1]);
                }
            }
        }
        for (Oilsensor lastrecord : lastrecordlist) {
            currrecord.setDeviceaddr(lastrecord.getDeviceaddr());
            String alm = "";
            try {
                currrecord.setLastch4(
                        read61850FloatValue(String.format(isnull(map61850.get("CH4ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastc2h6(read61850FloatValue(
                        String.format(isnull(map61850.get("C2H6ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastc2h4(read61850FloatValue(
                        String.format(isnull(map61850.get("C2H4ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastc2h2(read61850FloatValue(
                        String.format(isnull(map61850.get("C2H2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastco(
                        read61850FloatValue(String.format(isnull(map61850.get("COppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastco2(
                        read61850FloatValue(String.format(isnull(map61850.get("CO2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLasto2(
                        read61850FloatValue(String.format(isnull(map61850.get("O2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastn2(
                        read61850FloatValue(String.format(isnull(map61850.get("N2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLasth2(
                        read61850FloatValue(String.format(isnull(map61850.get("H2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLasth2o(
                        read61850FloatValue(String.format(isnull(map61850.get("Mst")), lastrecord.getDeviceaddr())));
                currrecord.setLastppm(currrecord.getLasth2o());
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("CH4Alm1")), lastrecord.getDeviceaddr())) == 1 ? "甲烷高报," : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("CH4Alm2")), lastrecord.getDeviceaddr())) == 1 ? "甲烷高高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H6Alm1")), lastrecord.getDeviceaddr())) == 1 ? "乙烷高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H6Alm2")), lastrecord.getDeviceaddr())) == 1 ? "乙烷高高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H4Alm1")), lastrecord.getDeviceaddr())) == 1 ? "乙烯高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H4Alm2")), lastrecord.getDeviceaddr())) == 1 ? "乙烯高高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H2Am1")), lastrecord.getDeviceaddr())) == 1 ? "乙炔高报," : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H2Am2")), lastrecord.getDeviceaddr())) == 1 ? "乙炔高高报,"
                                : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("COAlm1")), lastrecord.getDeviceaddr())) == 1
                        ? "一氧化碳高报,"
                        : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("COAlm2")), lastrecord.getDeviceaddr())) == 1
                        ? "一氧化碳高高报,"
                        : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("CO2Alm1")), lastrecord.getDeviceaddr())) == 1 ? "二氧化碳高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("CO2Alm2")), lastrecord.getDeviceaddr())) == 1 ? "二氧化碳高高报,"
                                : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("O2Alm1") ),
                lastrecord.getDeviceaddr())) == 1 ? "氧气高报," : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("O2Alm2") ),
                lastrecord.getDeviceaddr())) == 1 ? "氧气高高报," : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("N2Alm1") ),
                lastrecord.getDeviceaddr())) == 1 ? "氮气高报," : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("N2Alm2") ),
                lastrecord.getDeviceaddr())) == 1 ? "氮气高高报," : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("H2Alm1")), lastrecord.getDeviceaddr())) == 1
                        ? "氢气高报,"
                        : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("H2Alm2")), lastrecord.getDeviceaddr())) == 1
                        ? "氢气高高报,"
                        : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("MstAlm1")), lastrecord.getDeviceaddr())) == 1 ? "微水高报," : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("MstAlm2")), lastrecord.getDeviceaddr())) == 1 ? "微水高高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("SupDevRun")), lastrecord.getDeviceaddr())) == 1 ? "运行异常,"
                                : "";
                currrecord.setLastcommunicationstatus(read61850IntValue(
                        String.format(isnull(map61850.get("MoDevConf")), lastrecord.getDeviceaddr())));
            } catch (SocketException e) {
                if (Close()) {
                    oilwlserviceimpl.UpdateStatus(ad, 1);
                    ComRunner.netmap.remove(ad.getId());
                }
            } catch (Exception e) {
                setText("读取数据出错！" + e.toString());
            }

            if (alm.equals("")) {
                currrecord.setLastalarmlevel(0);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(),lastrecord.getDevicegroup(), lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), "", false);
                setText("无报警");
            } else {
                currrecord.setLastalarmlevel(2);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(),lastrecord.getDevicegroup(), lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), alm, true);
                setText(alm);
            }

            currrecord.setLastdatatime(new Date());
            oilserviceimpl.Update(currrecord, false);
            Oilhistory transhis = new Oilhistory();
            transhis.setStationid(currrecord.getStationid());
            transhis.setDevicegroup(currrecord.getDevicegroup());
            transhis.setDeviceaddr(currrecord.getDeviceaddr());
            transhis.setPointname(currrecord.getPointname());
            transhis.setH2(currrecord.getLasth2());
            transhis.setCo(currrecord.getLastco());
            transhis.setCh4(currrecord.getLastch4());
            transhis.setCo2(currrecord.getLastco2());
            transhis.setC2h4(currrecord.getLastc2h4());
            transhis.setC2h2(currrecord.getLastc2h2());
            transhis.setC2h6(currrecord.getLastc2h6());
            transhis.setTothyd(currrecord.getLasttothyd());
            if (currrecord.getLastppm() == null) {
                transhis.setPpm(currrecord.getLastppm());
            } else {
                transhis.setPpm(currrecord.getLastppm());
            }
            transhis.setO2(currrecord.getLasto2());
            transhis.setN2(currrecord.getLastn2());
            transhis.setDatatime(currrecord.getLastdatatime());
            transhis.setAlarmlevel(currrecord.getLastalarmlevel());
            transhis.setAlarmmessage(currrecord.getLastalarmmessage());
            transhis.setElem(currrecord.getLastelem());
            transhis.setProcesseddata(currrecord.getLastprocesseddata());
            transhis.setCommunicationstatus(currrecord.getLastcommunicationstatus());
            if (oilserviceimpl.Add(transhis) == 0) {
                setText("添加历史数据失败，请检查数据库连接");
            }           
            StringBuilder str2 = new StringBuilder();
            str2.append("【设备返回数据：】");
            str2.append(" 站点地址：" + currrecord.getStationid());
            str2.append(" 设备地址：" + currrecord.getDeviceaddr() + "\r\n");
            str2.append(" 氢气：" + currrecord.getLasth2() + "\r\n");
            str2.append(" 一氧化碳：" + currrecord.getLastco() + "\r\n");
            str2.append(" 甲烷：" + currrecord.getLastch4() + "\r\n");
            str2.append(" 二氧化碳：" + currrecord.getLastco2() + "\r\n");
            str2.append(" 乙烯：" + currrecord.getLastc2h4() + "\r\n");
            str2.append(" 乙烷：" + currrecord.getLastc2h6() + "\r\n");
            str2.append(" 乙炔：" + currrecord.getLastc2h2() + "\r\n");
            str2.append(" 总烃：" + currrecord.getLasttothyd() + "\r\n");
            str2.append(" 微水：" + currrecord.getLastppm() + "\r\n");
            str2.append(" 氧气：" + currrecord.getLasto2() + "\r\n");
            str2.append(" 氮气：" + currrecord.getLastn2() + "\r\n");
            setText(str2.toString());
        }
    }
    private void read61850(String str) {
        Oilwlsensor currrecord = new Oilwlsensor();
        currrecord.setStationid(ad.getStationid());
        currrecord.setDevicegroup(ad.getDevicegroup());
        List<Oilwlsensor> lastrecordlist = oilwlserviceimpl.getLists(currrecord);
        if (lastrecordlist.size() != 0) {
            if (lastrecordlist.get(0).getStyle61850() != null) {
                String[] style = lastrecordlist.get(0).getStyle61850().split(",");
                for (String s : style) {
                    map61850.put(s.split("\\|")[0], s.split("\\|")[1]);
                }
            }
        }
        for (Oilwlsensor lastrecord : lastrecordlist) {
            currrecord.setDeviceaddr(lastrecord.getDeviceaddr());
            String alm = "";
            try {
                currrecord.setLastch4(
                        read61850FloatValue(String.format(isnull(map61850.get("CH4ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastc2h6(read61850FloatValue(
                        String.format(isnull(map61850.get("C2H6ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastc2h4(read61850FloatValue(
                        String.format(isnull(map61850.get("C2H4ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastc2h2(read61850FloatValue(
                        String.format(isnull(map61850.get("C2H2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastco(
                        read61850FloatValue(String.format(isnull(map61850.get("COppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastco2(
                        read61850FloatValue(String.format(isnull(map61850.get("CO2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLasto2(
                        read61850FloatValue(String.format(isnull(map61850.get("O2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLastn2(
                        read61850FloatValue(String.format(isnull(map61850.get("N2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLasth2(
                        read61850FloatValue(String.format(isnull(map61850.get("H2ppm")), lastrecord.getDeviceaddr())));
                currrecord.setLasth2o(
                        read61850FloatValue(String.format(isnull(map61850.get("Mst")), lastrecord.getDeviceaddr())));
                currrecord.setLastppm(currrecord.getLasth2o());
                currrecord.setLasth2o(
                        read61850FloatValue(String.format(isnull(map61850.get("H2OTmp")), lastrecord.getDeviceaddr())));
                currrecord.setLasttothyd(
                        read61850FloatValue(String.format(isnull(map61850.get("CmbuGas")), lastrecord.getDeviceaddr())));

                alm += read61850IntValue(
                        String.format(isnull(map61850.get("CH4Alm1")), lastrecord.getDeviceaddr())) == 1 ? "甲烷高报," : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("CH4Alm2")), lastrecord.getDeviceaddr())) == 1 ? "甲烷高高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H6Alm1")), lastrecord.getDeviceaddr())) == 1 ? "乙烷高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H6Alm2")), lastrecord.getDeviceaddr())) == 1 ? "乙烷高高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H4Alm1")), lastrecord.getDeviceaddr())) == 1 ? "乙烯高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H4Alm2")), lastrecord.getDeviceaddr())) == 1 ? "乙烯高高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H2Am1")), lastrecord.getDeviceaddr())) == 1 ? "乙炔高报," : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("C2H2Am2")), lastrecord.getDeviceaddr())) == 1 ? "乙炔高高报,"
                                : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("COAlm1")), lastrecord.getDeviceaddr())) == 1
                        ? "一氧化碳高报,"
                        : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("COAlm2")), lastrecord.getDeviceaddr())) == 1
                        ? "一氧化碳高高报,"
                        : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("CO2Alm1")), lastrecord.getDeviceaddr())) == 1 ? "二氧化碳高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("CO2Alm2")), lastrecord.getDeviceaddr())) == 1 ? "二氧化碳高高报,"
                                : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("O2Alm1") ),
                    lastrecord.getDeviceaddr())) == 1 ? "氧气高报," : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("O2Alm2") ),
                    lastrecord.getDeviceaddr())) == 1 ? "氧气高高报," : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("N2Alm1") ),
                    lastrecord.getDeviceaddr())) == 1 ? "氮气高报," : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("N2Alm2") ),
                    lastrecord.getDeviceaddr())) == 1 ? "氮气高高报," : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("H2Alm1")), lastrecord.getDeviceaddr())) == 1
                        ? "氢气高报,"
                        : "";
                alm += read61850IntValue(String.format(isnull(map61850.get("H2Alm2")), lastrecord.getDeviceaddr())) == 1
                        ? "氢气高高报,"
                        : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("MstAlm1")), lastrecord.getDeviceaddr())) == 1 ? "微水高报," : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("MstAlm2")), lastrecord.getDeviceaddr())) == 1 ? "微水高高报,"
                                : "";
                alm += read61850IntValue(
                        String.format(isnull(map61850.get("SupDevRun")), lastrecord.getDeviceaddr())) == 1 ? "运行异常,"
                                : "";
                currrecord.setLastcommunicationstatus(read61850IntValue(
                        String.format(isnull(map61850.get("MoDevConf")), lastrecord.getDeviceaddr())));
            } catch (SocketException e) {
                if (Close()) {
                    oilwlserviceimpl.UpdateStatus(ad, 1);
                    ComRunner.netmap.remove(ad.getId());
                }
            } catch (Exception e) {
                setText("读取数据出错！" + e.toString());
            }

            if (alm.equals("")) {
                currrecord.setLastalarmlevel(0);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(),lastrecord.getDevicegroup(), lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), "", false);
                setText("无报警");
            } else {
                currrecord.setLastalarmlevel(2);
                currrecord.setLastalarmmessage(alm);
                GetNormalAlarm("油色谱" + lastrecord.getDeviceaddr(),lastrecord.getDevicegroup(), lastrecord.getDeviceaddr(), 0,
                        lastrecord.getPointname(), alm, true);
                setText(alm);
            }

            currrecord.setLastdatatime(new Date());
            currrecord.setLasthisdatatime(addHistory(lastrecord));
            oilwlserviceimpl.Update(currrecord, false);
            StringBuilder str2 = new StringBuilder();
            str2.append("【设备返回数据：】");
            str2.append(" 站点地址：" + currrecord.getStationid());
            str2.append(" 设备地址：" + currrecord.getDeviceaddr() + "\r\n");
            str2.append(" 氢气：" + currrecord.getLasth2() + "\r\n");
            str2.append(" 一氧化碳：" + currrecord.getLastco() + "\r\n");
            str2.append(" 甲烷：" + currrecord.getLastch4() + "\r\n");
            str2.append(" 二氧化碳：" + currrecord.getLastco2() + "\r\n");
            str2.append(" 乙烯：" + currrecord.getLastc2h4() + "\r\n");
            str2.append(" 乙烷：" + currrecord.getLastc2h6() + "\r\n");
            str2.append(" 乙炔：" + currrecord.getLastc2h2() + "\r\n");
            str2.append(" 总烃：" + currrecord.getLasttothyd() + "\r\n");
            str2.append(" 微水：" + currrecord.getLastppm() + "\r\n");
            str2.append(" 氧气：" + currrecord.getLasto2() + "\r\n");
            str2.append(" 氮气：" + currrecord.getLastn2() + "\r\n");
            setText(str2.toString());
        }
    }

    private void ZSH(String str) {
        setText(str);
        if (!str.substring(2, 4).equals("04") || currcmd >= 2) {
            return;
        }
        String addrstr = str.substring(0, 2);
        Integer addrint = parsePHex4(addrstr);
        reSend[addrint] = 0;

        Oilwlsensor currrecord = new Oilwlsensor();
        currrecord.setStationid(ad.getStationid());
        currrecord.setDevicegroup(ad.getDevicegroup());
        currrecord.setDeviceaddr(addrint);
        Oilwlsensor lastrecord = oilwlserviceimpl.getModel(currrecord);
        int index = 6;
        currrecord.setLasth2(IEEE754Utils.strbytesToSingle(str.substring(index, index + 8)).floatValue());
        index += 8;
        currrecord.setLastppm(IEEE754Utils.strbytesToSingle(str.substring(index, index + 8)).floatValue());
        index += 8;
        Random rd = new Random();
        currrecord.setLastppm(rd.nextFloat(1f) + 6f);
        String alarmMessage = "";// 报警消息
        if (lastrecord.getH2warnvalue() == null || lastrecord.getCowarnvalue() == null
                || lastrecord.getCh4warnvalue() == null
                || lastrecord.getCo2warnvalue() == null || lastrecord.getC2h4warnvalue() == null
                || lastrecord.getC2h2warnvalue() == null || lastrecord.getC2h6warnvalue() == null
                || lastrecord.getTothydwarnvalue() == null || lastrecord.getPpmalarmvalue() == null
                || lastrecord.getO2warnvalue() == null || lastrecord.getN2alarmvalue() == null
                || lastrecord.getH2warnvalue() == null || lastrecord.getCoalarmvalue() == null
                || lastrecord.getCh4alarmvalue() == null || lastrecord.getCo2alarmvalue() == null
                || lastrecord.getC2h4alarmvalue() == null || lastrecord.getC2h2alarmvalue() == null
                || lastrecord.getC2h6alarmvalue() == null || lastrecord.getTothydalarmvalue() == null
                || lastrecord.getPpmalarmvalue() == null
                || lastrecord.getO2alarmvalue() == null
                || lastrecord.getN2alarmvalue() == null) {
            setText("当前监测点：" + (lastrecord.getDeviceaddr()) + " 未设置报警值！");
            return;
        }
        int[] AlarmStatus = new int[2];
        AlarmStatus[0] = GetAlarmStatus(isnull(currrecord.getLasth2()), lastrecord.getH2alarmvalue(),
                lastrecord.getH2alarmrelievevalue(),
                lastrecord.getH2warnvalue(), lastrecord.getH2warnrelievevalue(), lastrecord.getLasth2alarmstatus(),
                true, "氢气",
                lastrecord.getDevicegroup(), lastrecord.getDeviceaddr(), lastrecord.getPointname());
        AlarmStatus[1] = GetAlarmStatus(isnull(currrecord.getLastppm()), lastrecord.getPpmalarmvalue(),
                lastrecord.getPpmalarmrelievevalue(),
                lastrecord.getPpmwarnvalue(), lastrecord.getPpmwarnrelievevalue(), lastrecord.getLastppmalarmstatus(),
                true, "微水",
                lastrecord.getDevicegroup(), lastrecord.getDeviceaddr(), lastrecord.getPointname());
        alarmMessage += GetAlarmMessage(AlarmStatus[0], "氢气");
        alarmMessage += GetAlarmMessage(AlarmStatus[1], "微水");

        currrecord.setLastalarmlevel(HexUtil.MAX(AlarmStatus));
        currrecord.setLastalarmmessage(alarmMessage);

        currrecord.setLastcommunicationstatus(0);
        currrecord.setLastdatatime(new Date());
        currrecord.setLasthisdatatime(addHistory(lastrecord));
        oilwlserviceimpl.Update(currrecord, false);
        StringBuilder str2 = new StringBuilder();
        str2.append("【设备返回数据：】");
        str2.append(" 变电站地址：" + currrecord.getStationid());
        str2.append(" 设备组：" + currrecord.getDevicegroup());
        str2.append(" 设备地址：" + currrecord.getDeviceaddr() + "\r\n");
        str2.append(" 氢气：" + currrecord.getLasth2() + "\r\n");
        str2.append(" 微水：" + currrecord.getLastppm() + "\r\n");
        setText(str2.toString());
        addrindex++;
    }



    private Date addHistory(Oilwlsensor lastrecord) {
        if (lastrecord != null) {
            if (lastrecord.getLasthisdatatime() == null) {
                add(lastrecord);
                return new Date();
            } else {
                long lasttime = 0;
                long nowtime = 0;
                try {
                    lasttime = HexUtil.todate(lastrecord.getLasthisdatatime()).getTime();
                    nowtime = HexUtil.todate(new Date()).getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int timersp = 60000;
                if (lastrecord.getLastalarmlevel() != 0) {
                    timersp = ad.getHisalarmtimersp() * 1000;
                } else {
                    timersp = ad.getHistimersp() * 1000;
                }
                if (lasttime + timersp < nowtime) {
                    add(lastrecord);
                    return new Date();
                }
            }
        }
        return null;
    }

    private void add(Oilwlsensor lastrecord) {
        Oilwlhistory lastrecordhis = new Oilwlhistory();
        lastrecordhis.setStationid(lastrecord.getStationid());
        lastrecordhis.setDevicegroup(lastrecord.getDevicegroup());
        lastrecordhis.setDeviceaddr(lastrecord.getDeviceaddr());
        lastrecordhis.setPointname(lastrecord.getPointname());
        lastrecordhis.setAlarmmessage(lastrecord.getLastalarmmessage());
        lastrecordhis.setAlarmlevel(lastrecord.getLastalarmlevel());
        lastrecordhis.setDatatime(lastrecord.getLastdatatime());
        lastrecordhis.setCh4(lastrecord.getLastch4());
        lastrecordhis.setC2h6(lastrecord.getLastc2h6());
        lastrecordhis.setC2h4(lastrecord.getLastc2h4());
        lastrecordhis.setC2h2(lastrecord.getLastc2h2());
        lastrecordhis.setCo(lastrecord.getLastco());
        lastrecordhis.setCo2(lastrecord.getLastco2());
        lastrecordhis.setO2(lastrecord.getLasto2());
        lastrecordhis.setN2(lastrecord.getLastn2());
        lastrecordhis.setH2(lastrecord.getLasth2());
        lastrecordhis.setH2o(lastrecord.getLasth2o());
        lastrecordhis.setPpm(lastrecord.getLastppm());
        lastrecordhis.setTothyd(lastrecord.getLasttothyd());
        oilwlserviceimpl.Add(lastrecordhis);
    }

    @Async("threadPool")
    public void startSendThread(Appdevice Appdevice) throws Exception {
        String cmd[] = Appdevice.getCmd().split(",");
        // cmd[0] = "010301300014";// 气体
        // cmd[1] = "010301900014";// 遥信
        checkType = "crc16";
        issend.put(Thread.currentThread().getId(), true);
        String cstr = "";
        while (issend.get(Thread.currentThread().getId()) != null) {
            rlock.lock();
            try {
                if (isrun) {
                    if (addrindex > Appdevice.getAddrmax() || addrindex < Appdevice.getAddrmin())
                        addrindex = Appdevice.getAddrmin();
                    currcmd = 0;
                    for (int i = 0; i < cmd.length; i++) {
                        if (currcmd == 0) {
                            ClearCache();
                        }
                        if(ad.getBrand().equals("白水泉"))
                        {

                        }else{
                        cstr = String.format(cmd[currcmd], String.format("%02X", addrindex));                            
                        }
                        

                        cstr = cstr + CRC16.calcCrc16(cstr);
                        Send(cstr);
                        sendCondition.await(timeoutsp, TimeUnit.MILLISECONDS);
                        currcmd++;
                    }

                    if (reSend[addrindex] > 3) {
                        oilwlserviceimpl.UpdateStatus(ad, addrindex, 1);
                        reSend[addrindex] = 0;
                        addrindex++;
                    }
                    reSend[addrindex]++;
                } else if (rescode != 0) {
                    setText(resmsg[rescode]);
                }
            } catch (Exception e) {
                // TODO: handle exception
            } finally {
                rlock.unlock();
            }
        }
    }
}
